<?php

class LoginInfo{
	var $userInfo=false;
	var $sessionKey='LoginInfoApkel';
	var $path = '';

	var $_user_table = 'pengguna';
	var $_group_table = 'group_pengguna';

	public function init()
	{
		$this->CI =& get_instance();
		$this->userInfo = $this->CI->session->userdata($this->sessionKey);
		$this->CI->LoginInfo = $this;

		$this->CI->load->vars('ci',$this->CI);

		$acl_use_db = $this->CI->config->item('acl_gid_use_db');
		$this->_user_table = $this->CI->config->item('acl_user_table');

		if($acl_use_db)
		{
			$this->CI->load->model('login_model');
			$this->_group_table = $this->CI->config->item('acl_gid_table');
			$gid = $this->CI->login_model->get_gid();

			
			//print_r($gid);
			$this->CI->config->set_item('acl_gid',$gid);
		}
		if(!$this->userInfo){
			$this->SetGuest('Tamu');
		}

		$exception_path = $this->CI->config->item('acl_public_site');


		$this->path = $this->CI->uri->segment(1).'/'.$this->CI->uri->segment(2);
		$this->landing_page = $this->path;

		//die($this->path);

		if(in_array($this->path, $exception_path))
		{

			return $this->AuthenticationPassed();	
		}
		else
		{
			if($this->userInfo['tipe_admin']=='Guest'){
				$this->AuthenticationFailed();
			}
			else{
				return $this->AuthenticationPassed();
			}
		}
		return $this->AuthenticationFailed();
	}

	public function SetGuest($name='Guest')
	{
		$Login = array(
			'id'		=>'0',
			'name' 		=> $name,
			'tipe_admin' 	=> 'Guest',
			'masuk_terakhir' => date('Y-m-d H:i:s',time())
		);
		$this->SetUserInfo($Login);
	}
	public function GetUserInfo($key=0)
	{
		if($key)
			return trim($this->userInfo[$key]);
		return $this->userInfo;
	}
	public function SetUserInfo($data)
	{
		$data['masuk_terakhir'] = date('Y-m-d H:i:s',time());
		$this->CI->session->set_userdata($this->sessionKey,$data);
		
		// if($data['id'])
		// 	$this->CI->db->where('id',$data['id'])->update($this->_user_table,array('masuk_terakhir'=>$data['masuk_terakhir']));

		$this->userInfo = $data;
	}
	public function UnsetUserInfo()
	{
		$data['keluar_terakhir'] = date('Y-m-d H:i:s',time());

		// if($this->userInfo['id'])
		// 	$this->CI->db->where('id',$this->userInfo['id'])->update($this->_user_table,array('keluar_terakhir'=>$data['keluar_terakhir']));
	
		$this->userInfo = false;
		$this->CI->session->unset_userdata($this->sessionKey);
	}
	public function Redirect()
	{
		if ($this->CI->input->is_ajax_request()) {
		   exit('login_required');
		}
		redirect('session/login');
	}
	public function LandingPage()
	{
		if($this->landing_page != 'session/login' )
			redirect($this->landing_page);
		else
			redirect('welcome/greeting');
	}
	public function AuthenticationPassed()
	{
		$acl_gid = $this->CI->config->item('acl_gid') ;
		
		// print_r($acl_gid);
		// echo 'X';
		// die();

		$acl_cls 	= $this->CI->uri->segment(1);
		$acl_fn 	= $this->CI->uri->segment(2);
		
	
		$gid = $acl_gid[trim($this->userInfo['tipe_admin'])]; 

		// print_r($this->userInfo);
		// die();

		$acl_cls = empty($acl_cls) ? '/' : $acl_cls;
		
		if( $acl_cls == '/' && $acl_fn == '')
		{
			$acl_fn = 'ep';
		}
		else if( $acl_fn == '' )
		{
			$acl_fn = 'index';
		}

		if($acl_cls != '')
		{
			$acl_registry = $this->CI->config->item('acl_registry') ;
			$reg = $acl_registry[$acl_cls][$acl_fn];
			if(!empty($reg))
			{
				if(in_array($gid, $reg))
				{
					// LOG ALL TRAFFIC HERE
				}
				else
				{
					$this->CI->load->add_view_path(APPPATH.'views/');
					$r = $this->CI->load->view('developer/output/views/session/ACCESS_TO_UNRESTRICTED_MODULES',false,true);
					//die('We are sorry but you dont have access to this resource.');
					die($r);
				}
			}
			else
			{
				$this->CI->load->add_view_path(APPPATH.'views/');
				$r = $this->CI->load->view('developer/output/views/session/ACCESS_TO_UNREGISTERED_MODULES',false,true);
				die($r);
				//die('Error : Access to unregistered modules...');
			}
		}
	}
	public function AuthenticationFailed()
	{
		$this->Redirect();
	}
	public function IsLoggedIn()
	{
		return $this->GetUserInfo('tipe_admin') != 'Guest';
	}
	public function IsAdmin()
	{
		return $this->GetUserInfo('tipe_admin') == 'Admin';
	}
	public function IsSuperUser()
	{
		return $this->GetUserInfo('tipe_admin') == 'Super User';
	}

	public function IsUser()
	{
		return $this->GetUserInfo('tipe_admin') == 'User';
	}
	public function IsGuest()
	{
		return $this->GetUserInfo('tipe_admin') == 'Guest';
	}
	public function IsMonkey()
	{
		return $this->GetUserInfo('tipe_admin') == 'Monkey';
	}
	public function Is($group)
	{
		return $this->GetUserInfo('tipe_admin') == $group;
	}
	public function encrypt_password($password)
	{
		return md5('**MARI WUJUDKAN INDONESIA BEBAS MIRAS DAN PERJUDIAN**'.$password.'MIRAS');
	}
	public function validate_password($password) {
	    return true;//!preg_match('#[^a-zA-Z0-9]#', $password);
	}
	
} 